Programming Ruby: The Pragmatic Programmers' Guide
Author:David Thomas; Chad Fowler; Andrew Hunt
Language: eng
Format: mobi
Tags: Non-fiction, COMPUTERS / Programming Languages / Ruby, Reference
ISBN: 9780974514055
Publisher: Pragmatic Bookshelf
Published: 2000-12-15T10:00:00+00:00
# this can be mixed in but the variable x won't be visible
def no_bar
return x
end
def bar
@x = 1000
return @x
end
puts( "In Foo: x = #{x}" ) # this can access the module-local x
end
include Foo # mix in the Foo module
When you run this program, the puts method executes when the module is initialized, and it displays the value of the module-local variable x:
In Foo: x = 50
If you display the x variable within the main scope of the program, the value of the variable x local to the main scope of the program is used, not the value of the variable x local to the module:
puts(x) #=> 1
But any attempt to execute the no_bar method will fail:
puts( no_bar ) # Error: undefined local variable or method 'x'
Here the no_bar method is unable to access either of the local variables named x even though x is declared both in the scope of the module (x = 50) and in the current or “main” scope (x = 1). But there is no such problem with instance variables. The bar method is able to return the value of the instance variable @x:
puts(bar) #=> 1000
A module may have its own instance variables that belong exclusively to the module “object.” These instance variables will be in scope to a module method:
inst_class_vars.rb
module X
@instvar = "X's @instvar"
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(11943)
Hello! Python by Anthony Briggs(9401)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9365)
The Mikado Method by Ola Ellnestam Daniel Brolund(9331)
Dependency Injection in .NET by Mark Seemann(8881)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7876)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7370)
Grails in Action by Glen Smith Peter Ledbrook(7314)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7141)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5982)
Kotlin in Action by Dmitry Jemerov(4663)
Practical Vim (for Kathryn Amaral) by Drew Neil(3734)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3609)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3477)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3261)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2958)
Learning Java by Patrick Niemeyer & Daniel Leuck(2878)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2853)
Microservices with Go by Alexander Shuiskov(2747)